package com.ymatou.iapi.livebuyer.testcase;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.SendLiveActivitySubscribeMessageBean;
import com.ymatou.iapi.livebuyer.service.SendLiveActivitySubscribeMessageCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveActivitySubscribe;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*发送秒杀活动订阅通知
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SendLiveActivitySubscribeMessage{
	private static SendLiveActivitySubscribeMessageBean sendliveactivitysubscribemessageBean;
	private static SendLiveActivitySubscribeMessageCall sendliveactivitysubscribemessageCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("发送秒杀活动订阅通知");
	}

	@Before
	public void caseUp() {
		sendliveactivitysubscribemessageBean=new SendLiveActivitySubscribeMessageBean();
		sendliveactivitysubscribemessageCall=new SendLiveActivitySubscribeMessageCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sendliveactivitysubscribemessageCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SendLiveActivitySubscribeMessage_001() {
		Logger.start(true,"发送秒杀活动订阅通知查询成功");
		try {
			LiveRoomWapper LiveRoomWapper=new LiveRoomWapper();
			int uid=2;
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 4*60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			int apid=LiveRoomCallService.Subscribeactivity(uid, createlive.getId(), liveacctivityid,productId);
			
			LiveActivitySubscribe DbLiveActivitySubscribe = LiveRoomWapper.selectLiveActivitySubscribeByExample(apid);
			Logger.verifyEquals(1, DbLiveActivitySubscribe.getState(), "验证status订阅");
			Logger.verifyEquals(0, DbLiveActivitySubscribe.getNotifyState(), "验证NotifyState订阅");
			Thread.sleep(3000);
			sendliveactivitysubscribemessageBean.setAppId("autotest");
			sendliveactivitysubscribemessageCall.setData(sendliveactivitysubscribemessageBean);
			sendliveactivitysubscribemessageCall.callService();			
			Logger.verifyEquals("ok", sendliveactivitysubscribemessageCall.getReturnData(), "验证返回");
			Thread.sleep(3000);
			DbLiveActivitySubscribe = LiveRoomWapper.selectLiveActivitySubscribeByExample(apid);
			Logger.verifyEquals(1, DbLiveActivitySubscribe.getNotifyState(), "验证NotifyState订阅");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}